package com.xck.clouddisk.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.xck.clouddisk.entity.AliMsgEntity;
import com.xck.clouddisk.service.AliMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AliMsgServiceImpl implements AliMsgService {
    @Autowired
    private AliMsgEntity aliMsgEntity;

    /**
     * 阿里云短信验证
     *
     * @param phone         电话号
     * @param templateParam 验证码,格式为{code:生成的验证码}
     * @return boolean
     * @date 2020/8/7 15:05
     */
    @Override
    public boolean sendSms(String phone, String templateParam) {
        CommonResponse response = null;
        //记得换成阿里oss存储的keyid和密钥
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GGW5dwfVA1eCNkUAzvQ", "bG7pPeOUeJE3ZCdl4SktcxN8yJE07R");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        System.out.println(aliMsgEntity.toString());
        System.out.println(phone);
        request.putQueryParameter("RegionId", aliMsgEntity.getRegionId());
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", aliMsgEntity.getSignName());
        request.putQueryParameter("TemplateCode", aliMsgEntity.getTemplateCode());
        request.putQueryParameter("TemplateParam", templateParam);
        try {
            response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
        }
        return true;
    }
}
